var app = getApp()
var Utils = require('../../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    data_arr: [],
    inputtext: '',
    upload_picture_list:[],
    inquiry:""
  },
  handleClickNext(){
    wx.navigateTo({
      url: '/hyb_yl/zhuanjiasubpages/pages/huanzhexinxi/huanzhexinxi?inquiry=' + this.data.inquiry,
    })
  },
  /**   
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.channel = "";
    this.uid = Utils.getUid();
    this.lock = false;
    this.setData({
      inquiry: options.inquiry
    })
    let userInfo = wx.getStorageSync("userInfo");
    if (userInfo) {
      this.setData({
        hasUserInfo: true,
        userInfo: userInfo,
        u_name: userInfo.u_name,
        u_thumb: userInfo.u_thumb,
      });
    }
    var that = this
    var zid = options.zid
    var bgc = wx.getStorageSync('color')
    var keywords = options.keywords
    var name = options.name
    var randnum = options.randnum //专家房间号
    console.log(name, randnum)
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
    app.util.request({
      'url': 'entry/wxapp/Hzbingli.url',
      success: function(res) {
        console.log(res)
        that.setData({
          url: res.data,
        })
      }
    })
    app.util.request({
      'url': 'entry/wxapp/Zhuanjia.docinfo',
      data: {
        zid: zid,
        key: keywords
      },
      success: function(res) {
        console.log(res)
        that.setData({
          xiangqing: res.data.data,
          bgc: bgc,
          keywords: keywords,
          docopenid: res.data.data.openid,
          zid: res.data.data.zid,
          z_name: res.data.data.z_name,
          z_thumbs: res.data.data.z_thumbs,
          name: name,
          money: res.data.data.newmoney,
          randnum: randnum
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function(e) {
    var that = this
    let pages = getCurrentPages();
    let currPage = pages[pages.length - 1];
    if (currPage.data.names) {
      that.setData({ //将携带的参数赋值
        names: currPage.data.names
      });
    }
    if (currPage.data.j_id) {
      that.setData({ //将携带的参数赋值
        j_id: currPage.data.j_id
      });
    }
    if (currPage.data.tel) {
      that.setData({ //将携带的参数赋值
        tel: currPage.data.tel
      });
    }
    if (currPage.data.region && currPage.data.state == 1) {
      that.setData({ //将携带的参数赋值
        region: currPage.data.region
      });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },
  seluser() {
    var that = this
    var keywords = that.data.keywords
    wx.navigateTo({
      url: '/hyb_yl/zhuanjiasubpages/pages/huanzhexinxi/huanzhexinxi',
    })
  },

  nextbu(e) {
    var that = this
    var inputtext = that.data.inputtext
    var data_arr = that.data.data_arr
    var j_id = that.data.j_id
    var names = that.data.names
    var keywords = that.data.keywords
    var name = that.data.name
    var u_name = that.data.u_name
    var u_thumb = that.data.u_thumb
    var z_thumbs = that.data.z_thumbs
    var z_name = that.data.z_name
    var myroom = wx.getStorageSync('myUsername'); //自己的账号
    var docroom = that.data.randnum //医生的账号
    var money = that.data.money
    var zid = that.data.zid
    if (data_arr.length !== '') {
      var type = 0
    } else {
      var type = 1
    }
    if (inputtext == "") {
      wx.showToast({
        title: '请简单描述病情',
        icon: 'none'
      })
      return false;
    }
    
    app.util.request({
      'url': 'entry/wxapp/Tuwen.addbl',
      data: {
        msg: inputtext,
        data_arr: data_arr,
        u_name: u_name, //患者名称
        u_thumb: u_thumb, //患者头像
        z_thumbs: z_thumbs, //医生头像
        z_name: z_name, //医生名称
        useropenid: wx.getStorageSync('openid'), //患者openid
        docopenid: that.data.docopenid,
        j_id: j_id,
        keywords: keywords,
        type: type,
        myroom: myroom,
        docroom: docroom,
        names: names,
        name: name,
        zid: zid,
        money: money
      },
      success: function(res) {
        console.log(res)
        var order = res.data.orders
        wx.navigateTo({
          url: '/hyb_yl/czhuanjiasubpages/pages/questends/index?phone=' + that.data.tel + '&zid=' + zid + '&name=' + name + '&keywords=' + keywords + '&j_id=' + j_id + '&allone_key=2' + '&name=' + name + '&order=' + order + '&money=' + money + '&myroom=' + myroom + '&docroom=' + docroom
        })
      }
    })
  },
  uploadimg: function() {
    let that = this;
    var uniacid = app.siteInfo.uniacid
    var data_arr = that.data.upload_picture_list;
    wx.chooseImage({
      count: 5,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        for (var i = 0; i < tempFilePaths.length; i++) {
          wx.uploadFile({
            url: that.data.url + 'app/index.php?i=' + uniacid + '&c=entry&a=wxapp&do=Uploadback&m=hyb_yl',
            filePath: tempFilePaths[i],
            name: 'upfile',
            formData: [],
            success: function(res) {
              console.log(res)
              data_arr.push(res.data)
              that.setData({
                upload_picture_list: data_arr
              })
            }
          })
        }
      }
    })
  },

  deleteimg: function(e) {
    var that = this;
    var i = e.currentTarget.dataset.index;
    var imgArr = that.data.data_arr;
    imgArr.splice(i, 1);
    that.setData({
      upload_picture_list: imgArr
    })
  },
  computing_word(e) {
    var that = this
    var inputtext = e.detail.value
    that.setData({
      inputtext: inputtext
    })
  },
  selhuanzhe() {
    // zid = 54 & name=电话问诊 & keywords=dianhuawenzhen & j_id=2
    var that = this
    var zid = that.data.zid
    var name = that.data.name
    var keywords = that.data.keywords
    var j_id = that.data.j_id
    wx.navigateTo({
      url: '/hyb_yl/zhuanjiasubpages/pages/huanzhexinxi/huanzhexinxi?zid=' + zid + '&name=' + name + '&keywords=' + keywords + '&j_id=' + j_id,
    })
  }
})